import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/LiquidationMonitor.vue'),
    meta: {
      title: '爆仓监控'
    }
  },
  {
    path: '/monitor',
    name: 'Monitor',
    component: () => import('@/views/LiquidationMonitor.vue'),
    meta: {
      title: '爆仓监控'
    }
  },
  {
    path: '/stats',
    name: 'Stats',
    component: () => import('@/views/SystemStats.vue'),
    meta: {
      title: '系统统计'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: {
      title: '页面未找到'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 加密货币爆仓监控`
  }
  
  next()
})

router.afterEach((to, from) => {
  // 路由切换后的处理
  console.log(`路由从 ${from.path} 切换到 ${to.path}`)
})

export default router